<?php
/**
 * Nadëb (Makú-Nadëb)
 * 
 * @author     Mateus Martins <mateusweb@gmail.com>
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license    http://www.gnu.org/licenses/gpl.html | GPL
 * @package    Nadeb
 * @version    1.0.0
 */


/**
 * Class Nadeb_Login
 * Monta um array para o header html baseado em um arquivo ini
 * 
 * @category   Nadeb
 * @package    Nadeb_Login
 * @copyright  Copyright 2010 mateusweb.com.br
 * @license    http://www.gnu.org/licenses/gpl.html | GPL
 */
class Nadeb_Login
{
	protected static $_instance = null;
	 
	protected $usr;	
	protected $psw = "";
	
	private $table_name;
	private $user_name;
	private $user_psw;
	private $params = null;
	private $ses;
	
	protected function __construct()
	{
		$this->ses = new Zend_Session_Namespace();
	}
	
    /**
     * Retorna a instancia de Nadeb_Login
     * Implementação do Singleton pattern 
     *
     * @return Nadeb_Login
     */
    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }
    
    public function set_post($_params)
    {
    	$this->params = $_params;
    }
    	
	public function logar()
	{
		$filter = new Zend_Filter_StripTags();
		$this->usr    = $filter->filter( $this->params[$this->user_name] );
		$this->psw    = $filter->filter( $this->params[$this->user_psw] );
		
		if( $this->is_validRequest() )
		{
			$db = Zend_Registry::get("db");
			$authAdapter = new Zend_Auth_Adapter_DbTable($db);
			
			$authAdapter->setTableName( $this->table_name );
			$authAdapter->setIdentityColumn( $this->user_name );
			$authAdapter->setCredentialColumn( $this->user_psw );
			
			$authAdapter->setIdentity( $this->usr );
			$authAdapter->setCredential( $this->psw );
			$authAdapter->setCredentialTreatment( 'AES_ENCRYPT(?, "'.$this->psw.'")' );
			
			$auth = Zend_Auth::getInstance();  
			$authResult = $auth->authenticate($authAdapter);
			
			if( !$authResult->isValid() )
				$result = "Login incorreto";
			
			if( $authResult->isValid() )
			{
				$result         = "OK";
				$this->id       = $authAdapter->getResultRowObject();
				
				if( $this->id->active )
				{
					$this->ses->usr = $this->id; 
					$this->logou    = true;
				}
				else
				{
					$this->logou    = false;
					$result = "Usuário inativo";
				}
			}
		}
		else
			$result = $this->result;
		
		return $result;
	}
	
	public function get_info()
	{
		return $this->ses->usr;
	}
	
	public function set_tableName($_value)
	{
		return $this->table_name = $_value;
	}

	public function set_userName($_value)
	{
		return $this->user_name  = $_value;
	}
	
	public function set_userPsw($_value)
	{
		return $this->user_psw   = $_value;
	}
	
	private function is_validRequest()
	{
		if(!$this->psw)
		{
			$this->result = "Digite um usuário e senha!";
			return false;
		}

		if(!$this->usr)
		{
			$this->result = "Digite um usuário e senha!";
			return false;
		}
			
		if(($this->usr) && ($this->psw))
		{
			$this->result = "Iniciando autenticação...";
		}
			
		return true;
	}
	
	public function register_log()
	{
		$db = Zend_Registry::get("db"); 
		$db->insert("tb_user_login",Array(
			"log_date"   => date("Y-m-d H:i:s"),
			"log_ip"     => getenv("REMOTE_ADDR"),
			"use_id"	 => $this->id->use_id
		));
	}
	
	public function hasIdentity()
	{
		$auth = Zend_Auth::getInstance();
		if(!$auth->hasIdentity())
		{
			return false;
		}
		
		return true;
	}
}